From 65561b53344b834877e6b63320066a1e26038a3c Mon Sep 17 00:00:00 2001
From: Dario Binacchi <dario.binacchi@amarulasolutions.com>
Date: Fri, 9 Dec 2022 18:18:27 +0100
Subject: [PATCH] Fix getopt linking error

The buildroot project, to which the sscep application was added, has
configurations that raise the following linking error:
buildroot/output/host/lib/gcc/arc-buildroot-linux-uclibc/11.3.0/../../../../arc-buildroot-linux-uclibc/bin/ld: buildroot/output/host/bin/../arc-buildroot-linux-uclibc/sysroot/usr/lib/libc.a(getopt.os):(.data+0x8): multiple definition of `optind'; src/getopt.o:(.data+0x0): first defined here
buildroot/output/host/lib/gcc/arc-buildroot-linux-uclibc/11.3.0/../../../../arc-buildroot-linux-uclibc/bin/ld: buildroot/output/host/bin/../arc-buildroot-linux-uclibc/sysroot/usr/lib/libc.a(getopt.os): in function `__GI_getopt':
getopt.c:(.text+0x5a4): multiple definition of `getopt'; src/getopt.o:getopt.c:(.text+0x0): first defined here
buildroot/output/host/lib/gcc/arc-buildroot-linux-uclibc/11.3.0/../../../../arc-buildroot-linux-uclibc/bin/ld: buildroot/output/host/bin/../arc-buildroot-linux-uclibc/sysroot/usr/lib/libc.a(getopt.os): in function `getopt_long':
getopt.c:(.text+0x5b0): multiple definition of `getopt_long'; src/getopt.o:getopt.c:(.text+0x128): first defined here
collect2: error: ld returned 1 exit status
make[2]: *** [Makefile:507: sscep] Error 1
make[1]: *** [package/pkg-generic.mk:293: buildroot/output/build/sscep-0.10.0/.stamp_built] Error 2

The patch re-added a check that commit
81f56f635259b9 ("Replaced GNU getopt by a BSD licensed alternative")
removed.

Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
[yann.morin.1998@free.fr: make that an actual backport]
Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>
---
 src/getopt.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/getopt.c b/src/getopt.c
index eae36a6..0109406 100644
--- a/src/getopt.c
+++ b/src/getopt.c
@@ -31,6 +31,16 @@
 #include <stddef.h>
 #include <string.h>
 
+#define GETOPT_INTERFACE_VERSION 2
+#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
+# include <gnu-versions.h>
+# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
+#  define ELIDE_CODE
+# endif
+#endif
+
+#ifndef ELIDE_CODE
+
 char* optarg;
 int optopt;
 /* The variable optind [...] shall be initialized to 1 by the system. */
@@ -226,3 +236,5 @@ int getopt_long(int argc, char* const argv[], const char* optstring,
   ++optind;
   return retval;
 }
+
+#endif	/* Not ELIDE_CODE.  */
-- 
2.25.1

